//
//  LBPhotoTransition.m
//  app
//
//  Created by LINAICAI on 2017/5/12.
//  Copyright © 2017年 NAICAI LI. All rights reserved.
//

#import "LBPhotoTransition.h"
#import "LBPhotoBrowser.h"
#import "UIImage+LBPhotoBrowser.h"


@interface LBPhotoTransition()<UIViewControllerAnimatedTransitioning>
@property(nonatomic , assign)LBPhotoTransitionStyle style;
@property(nonatomic , assign)NSTimeInterval animationDuration;

@end
@implementation LBPhotoTransition
- (instancetype)initWithType:(LBPhotoTransitionStyle)style
{
    self = [super init];

    if (self) {
        self.style = style;
        self.animationDuration = 0.5;
    }
    return self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return  self.animationDuration;
}
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    UIView *containerView = transitionContext.containerView;
    switch (self.style) {
        case LBPhotoTransitionPresent:
        {
            //获取目标控制器，并且把视图添加到过度容器页中
            LBPhotoBrowser *toVc = (LBPhotoBrowser *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
            [containerView addSubview:toVc.view];
            //设置过度容器页透明
            toVc.view.alpha = 0;
            UIImageView *sourceImageView = toVc.sourceImageView;
            UIImage *sourceImage = sourceImageView.image;
            ///如果源图像为空
            if (!sourceImageView || !sourceImage) {
                [UIView animateWithDuration:[self transitionDuration:transitionContext]  animations:^{
                    toVc.view.alpha = 1.0;
                }completion:^(BOOL finished) {
                    [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                }];
                return;
            }
            UIImageView *imageView = [UIImageView new];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.image = sourceImage;
            imageView.frame = sourceImageView.superview?[sourceImageView.superview convertRect:sourceImageView.frame toView:toVc.view]:CGRectZero;
            [toVc.view insertSubview:imageView atIndex:1];
            ///开始过渡动画
            [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:5 initialSpringVelocity:0.25 options:UIViewAnimationOptionCurveEaseOut animations:^{
                toVc.view.alpha = 1.0;
                imageView.frame = [imageView.image lb_frameWithScreenWidth];
            } completion:^(BOOL finished) {
                if (finished) {
                    [toVc scrolToCurrentIndex:NO];
                    [imageView removeFromSuperview];
                    [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                }
                
                

            }];
        }
            break;
        case LBPhotoTransitionDismiss:
        {
            //获取源控制器
            LBPhotoBrowser *fromVC = (LBPhotoBrowser *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
            UIImageView *sourceImageView = fromVC.sourceImageView;
            UIImageView *displayImageView = fromVC.displayImageView;
            if (!sourceImageView || !displayImageView) {
                [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                    fromVC.view.alpha = 0;
                }completion:^(BOOL finished) {
                    [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                }];
            }
            else{
                displayImageView.frame = displayImageView.superview?[displayImageView.superview convertRect:displayImageView.frame toView:containerView]:CGRectZero;
                [containerView addSubview:displayImageView];
                [UIView animateWithDuration:[self transitionDuration:transitionContext]*0.5  delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                    fromVC.view.alpha = 0;
                    displayImageView.frame = sourceImageView.superview?[sourceImageView.superview convertRect:sourceImageView.frame toView:containerView]:CGRectZero;
                } completion:^(BOOL finished) {
                    [displayImageView removeFromSuperview];
                    if (finished) {
                        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
                    }

                }];
            }
        }
            break;
            
        default:
            break;
    }
}
@end
